package org.web.hello.dubbomarket.server.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.web.base.domain.ResultDO;
import org.web.base.domain.ViewResultHelper;
import org.web.base.domain.helper.ServiceExceptionHelper;
import org.web.base.domain.helper.ValidationHelper;
import org.web.base.domain.view.ViewResultDO;
import org.web.hello.dubbomarket.sdk.biz.AccountInfoBiz;
import org.web.hello.dubbomarket.sdk.dto.AccountInfoDTO;
import org.web.hello.dubbomarket.sdk.dto.query.QueryAccountInfoRequest;
import org.web.hello.dubbomarket.server.domain.AccountInfoDO;
import org.web.hello.dubbomarket.server.domain.query.QueryAccountInfo;
import org.web.hello.dubbomarket.server.service.AccountInfoService;

import javax.annotation.Resource;
import java.util.List;

@Controller()
@RequestMapping("dubboMarketServerAccountInfo")
public class AccountInfoController {

    private static final Logger logger = LogManager.getLogger(AccountInfoController.class);


    @Resource
    private AccountInfoBiz accountInfoBiz;

    @RequestMapping("/selectAccountInfoList")
    @ResponseBody
    public ViewResultDO<List<AccountInfoDTO>> selectAccountInfoList(QueryAccountInfoRequest queryAccountInfoRequest) {
        ViewResultDO<List<AccountInfoDTO>> viewResultDO;
        try {
            // 查询对象校验, 如果调用BIZ层方法，则无需校验，因为BIZ自会进行校验。
            ValidationHelper.validateWithThrow(QueryAccountInfoRequest.class, queryAccountInfoRequest);
            // 调用BIZ层方法
            QueryAccountInfo queryAccountInfo = new QueryAccountInfo();
            BeanUtils.copyProperties(queryAccountInfoRequest, queryAccountInfo);
            ResultDO<List<AccountInfoDTO>> selectResult = accountInfoBiz.selectAclResourceList(queryAccountInfoRequest);
            viewResultDO = ViewResultHelper.convertToViewResultByResultDO(selectResult, AccountInfoDTO.class);
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            viewResultDO = ViewResultHelper.buildViewResultByServiceException(e, null);
        }
        return viewResultDO;
    }

}
